﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Rmlife.Site;
using Rmlife.Model.Global;
namespace Rmlife.Web.Systems.cosmelist
{
    public partial class CosmeListMenu : System.Web.UI.Page
    {
        private string strCondition = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 绑定跟树结构：父节点
        /// </summary>
        private void BindTree(string Condition)
        {
            this.tvList.Nodes.Clear();

            IList list = Cosmes.GetTopList(Condition);

            foreach (XmlConfigInfo.ToptabmenuInfo tInfo in list)
            {
                TreeNode parentNode = new TreeNode();
                parentNode.Value = tInfo.mainmenuidlist.ToString();
                parentNode.Text = tInfo.title.ToString();
                parentNode.Target = "rightFrame";
                //parentNode.NavigateUrl = row["Url"] != null && !string.IsNullOrEmpty(row["Url"].ToString()) ? row["Url"].ToString() : "";
                parentNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                //parentNode.PopulateOnDemand = true;//是否动态添加子节点
                BindMain(parentNode, Condition);
                //if (parentNode.ChildNodes.Count > 0)
                //{
                this.tvList.Nodes.Add(parentNode);
                tvList.ShowExpandCollapse = true;
                //}
            }

            if (this.tvList.Nodes.Count <= 0)
            {
                tvList.Visible = false;
                msg.Text = "搜索完毕，没有任何记录";
                msg.Visible = true;
            }

        }
        /// <summary>
        /// 绑定商品：子节点
        /// </summary>
        /// <param name="tNode"></param>
        /// <param name="Condition"></param>
        public void BindMain(TreeNode tNode, string Condition)
        {
            string[] imainmenuidlist = tNode.Value.Split(',');

            IList listMain = Cosmes.GetMainMenuList(imainmenuidlist, Condition);

            foreach (XmlConfigInfo.MainmenuInfo mInfo in listMain)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = mInfo.menuid != null && !string.IsNullOrEmpty(mInfo.menuid.ToString()) ? mInfo.menuid.ToString() : "";

                // string emgUrl = "2.gif";
                /*
                if (null != row["DaZheProductId"] && !string.IsNullOrEmpty(row["DaZheProductId"].ToString()))
                {
                    int state = row["State"] != null && !string.IsNullOrEmpty(row["State"].ToString()) ? Convert.ToInt32(row["State"].ToString()) : 0;
                    switch (state)
                    {
                        case 0:
                            emgUrl = "3.gif";
                            break;
                        case 1:
                            emgUrl = "1.gif";
                            break;
                    }
                }
                 * */
                string checkstr = ""; //"<input type='checkbox' name='cbcheck' value='" + mInfo.menuid + "' />";
                childNode.Text = checkstr + " " + mInfo.menutitle.ToString() + "";
                childNode.ShowCheckBox = true;
                childNode.Target = "rightFrame";
                childNode.SelectAction = TreeNodeSelectAction.Expand;
                /*
                 *  绑定子菜单
                 */
                IList listMenu = Cosmes.GetSubMenuList(childNode.Value, Condition);
                foreach (XmlConfigInfo.SubmainInfo sInfo in listMenu)
                {
                    TreeNode menuNode = new TreeNode();
                    menuNode.Value = sInfo.link.ToString();
                    string chchild = "";// "<input type='checkbox' name='cbchild' value='" + sInfo.menuparentid + "' />";
                    menuNode.Text = chchild + " " + sInfo.menutitle.ToString();
                    menuNode.SelectAction = TreeNodeSelectAction.None;
                    menuNode.ShowCheckBox = true;
                    childNode.ChildNodes.Add(menuNode);
                }

                /* *
                 * 构造参数
                 * pn:产品名称 
                 * bn：品牌名称
                 * spec:规格
                 * spu：规格
                 * url：目标地址
                 **/
                /*
                string pn = row["ProductName"] != null && !string.IsNullOrEmpty(row["ProductName"].ToString()) ? row["ProductName"].ToString() : "";
                string bn = row["BrandName"] != null && !string.IsNullOrEmpty(row["BrandName"].ToString()) ? row["BrandName"].ToString() : "";
                string spec = row["Spec"] != null && !string.IsNullOrEmpty(row["Spec"].ToString()) ? row["Spec"].ToString() : "";
                string spu = row["SpecUnit"] != null && !string.IsNullOrEmpty(row["SpecUnit"].ToString()) ? row["SpecUnit"].ToString() : "";

                string url = "DaZheRelated.aspx";
                url += "?pid=" + ChinaBest.CosmeticsLibrary.Util.CommonLib.UrlEncode(childNode.Value);
                //url += "&pn=" + ChinaBest.CosmeticsLibrary.Util.CommonLib.UrlEncode(pn);
                //url += "&bn=" + ChinaBest.CosmeticsLibrary.Util.CommonLib.UrlEncode(bn);
                //url += "&spec=" + ChinaBest.CosmeticsLibrary.Util.CommonLib.UrlEncode(spec);
                //url += "&spu=" + ChinaBest.CosmeticsLibrary.Util.CommonLib.UrlEncode(spu);

                childNode.NavigateUrl = url;
                 * */
                tNode.ChildNodes.Add(childNode);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindTree(this.txtSearch.Text);
        }

        protected void tvList_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            //if (IsCallback)
            //    if (e.Node.Expanded != true || e.Node.ChildNodes.Count > 0)
            //    {
            //  LoadChildNode(e.Node);
            //}
            //else
            //{
            //    return;
            //}
            if (e.Node.Checked)
            {
                foreach (TreeNode n in e.Node.ChildNodes)
                {
                    n.Selected = true;
                }
            }
        }

        protected void LoadChildNode(TreeNode node)
        {
            try
            {
                BindMain(node, this.txtSearch.Text);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write("error:" + ex.StackTrace + ex.Message);
            }
        }
    }
}